(:
   Test
 :)

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
declare boundary-space strip;

(:
   Recherche de l'indice d'une chaine dans une liste de chaines
 :)
declare function local:cvn($itl as xs:string*, $elt as xs:string) as xs:int
{
    for $e at $p in $itl
    where $e = $elt
    return $p
}

(:
  Quelques variables globales
 :)
declare variable $docname := $arguments[1] ;
declare variable $gel := doc($docname)/trace/event[grapheur] ;
declare variable $gec := $gel[grapheur/argument[@action="create"]];
(:
  La liste des identifications de boites
:)
declare variable $boxes := 
   for $e in $gec
   where $e[grapheur/argument[@type="box"]]
   return
     string($e/grapheur/argument/@id) ;
(:
  La liste des identifications de cercles
:)
declare variable $arrows := 
   for $e in $gec
   where $e[grapheur/argument[@type="arrow"]]
   return
     string($e/grapheur/argument/@id) ;

(:
    Calcul effectif du resultat
 :)
<display>{
    (:
    (
    for $z in $boxes
    return
       <z>{$z, " =  box ", local:cvn($boxes, $z)}</z>
    ),
    (
    for $r in $arrows
    return
       <r>{$r, " = circle ", local:cvn($arrows, $r)}</r>
    ) :)

for $e at $p in $gel
where $e[grapheur]
return
  <item>{
    <info name="type">grapher</info> ,
    <info name="time">{ $e/time }</info>,
    <info name="src-anchor">{
      <anchor>{
        <doc>{ $docname }</doc>,
        <path>{jj:build-Path($e)}</path>
      }</anchor>
    }</info>,
    <info name="user">{string($e/@user)}</info>,
    <info name="room">{string($e/@room)}</info>,
    if ($e/grapheur/engaged) then
       if ($e/grapheur/engaged[@status="false"]) then
          ( <info name="engaged">false</info>,
            <info name="content">se désengage</info>)
       else 
          ( <info name="engaged">true</info>,
            <info name="content">s'engage</info>) 
    else
    if ($e/grapheur/argument) then
        (
             <info name="id">{string($e/grapheur/argument/@id)}</info>,
             if ($e/grapheur/argument[@action="create" and @type="box"]) then
                 ( <info name="op">create</info>,
                   <info name="content">crée la boîte {local:cvn($boxes,
                         string($e/grapheur/argument/@id) )}</info> )
             else
                ()
        )
    else
       ()
  }</item>

}</display>
